package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WorkArea;
import com.ruoyi.system.domain.WorkProfession;
import com.ruoyi.system.domain.WorkProfession;
import com.ruoyi.system.mapper.WorkProfessionMapper;
import com.ruoyi.system.mapper.WorkProfessionMapper;
import com.ruoyi.system.service.IWorkProfessionService;

/**
 * 【专业】Service业务层处理
 *
 * @author zwz
 * @date 2024-08-14
 */
@Service
public class WorkProfessionServiceImpl extends ServiceImpl<WorkProfessionMapper, WorkProfession> implements IWorkProfessionService
{
    @Autowired
    private WorkProfessionMapper workProfessionMapper;

    @Autowired
    WorkProfessionMapper WorkProfessionMapper;
    List<WorkProfession> all;
    /**
     * 查询专业
     *
     * @param id 专业主键
     * @return 专业
     */
    @Override
    public WorkProfession selectWorkProfessionById(Long id)
    {
        return workProfessionMapper.selectWorkProfessionById(id);
    }

    /**
     * 查询专业列表
     *
     * @param workProfession 专业
     * @return 专业
     */
    @Override
    public List<WorkProfession> selectWorkProfessionList(WorkProfession workProfession)
    {
        return workProfessionMapper.selectWorkProfessionList(workProfession);
    }

    /**
     * 新增专业
     *
     * @param workProfession 专业
     * @return 结果
     */
    @Override
    public int insertWorkProfession(WorkProfession workProfession)
    {
        workProfession.setCreateTime(DateUtils.getNowDate());
        return workProfessionMapper.insertWorkProfession(workProfession);
    }

    /**
     * 修改专业
     *
     * @param workProfession 专业
     * @return 结果
     */
    @Override
    public int updateWorkProfession(WorkProfession workProfession)
    {
        workProfession.setUpdateTime(DateUtils.getNowDate());
        return workProfessionMapper.updateWorkProfession(workProfession);
    }

    /**
     * 批量删除专业
     *
     * @param ids 需要删除的专业主键
     * @return 结果
     */
    @Override
    public int deleteWorkProfessionByIds(Long[] ids)
    {
        return workProfessionMapper.deleteWorkProfessionByIds(ids);
    }

    /**
     * 删除专业信息
     *
     * @param id 专业主键
     * @return 结果
     */
    @Override
    public int deleteWorkProfessionById(Long id)
    {
        return workProfessionMapper.deleteWorkProfessionById(id);
    }
    
    

	@Override
	public List<WorkProfession> getWorkProfessionTreeList(WorkProfession WorkProfession) {
		QueryWrapper<WorkProfession> queryWrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(WorkProfession.getParentCode())) {
			queryWrapper.eq("parent_code", WorkProfession.getParentCode());
		}

		
		List<WorkProfession> listTree = Lists.newArrayList();
		WorkProfession productClass = new WorkProfession();
		//productClass.setClassName("产品大类");
		//productClass.setClassId(0L);
		//productClass.setParentId(0L);
		//listTree.add(productClass);
		List<WorkProfession> nodeList = workProfessionMapper.selectList(queryWrapper);
		 QueryWrapper wa = new QueryWrapper();
		wa.orderByAsc("id");
		if(all == null) {
			   all = workProfessionMapper.selectList(wa);
	     }
		List<WorkProfession> list = Lists.newArrayList();
		for (WorkProfession node : nodeList) {
			getChildTree(node, list, all);
		}
		listTree.addAll(nodeList);
		return listTree;
	}
	
	/**
	 * 递归查找展开虚树型结构.
	 */
	public List<WorkProfession> getChildTree(WorkProfession root, List<WorkProfession> bomStructuresDTOList, List<WorkProfession> all) {
		// 获取当前root的子级
		String parentId = root.getProfessionCode();
		QueryWrapper<WorkProfession> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("parent_code", parentId);

		List<WorkProfession> nodeList = getChild(parentId, all);
		// 把子级循环一遍
		if(!nodeList.isEmpty()) {
			root.setChildren(nodeList);
		}
		for (WorkProfession node : nodeList) {

			bomStructuresDTOList.add(node);
			List<WorkProfession> child = getChildTree(node, bomStructuresDTOList, all);

		}

		// 递归退出条件没有下一层级
		if (nodeList == null || nodeList.size() == 0) {
			return Lists.newArrayList();
		}
		return nodeList;
	}
	public List<WorkProfession> getChild(String parentCode, List<WorkProfession> all){
		List list = Lists.newArrayList();
		for(WorkProfession a : all) {
			if(a.getParentCode().equals(parentCode)) {
				list.add(a);	
			}
		}
		return list;
	}
	public List<WorkProfession> getParentList(String name) {
		QueryWrapper<WorkProfession> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("profession_code",name);
		List<WorkProfession> list = WorkProfessionMapper.selectList(queryWrapper);
		List<WorkProfession> result = Lists.newArrayList() ;
		for(WorkProfession a : list) {
			getPrioTree(a,result);
		}
		Collections.reverse(result);
		return result;
	}
	
	/**
	 * 递归查找展开虚树型结构.
	 */
	public List<WorkProfession> getPrioTree(WorkProfession root, List<WorkProfession> bomStructuresDTOList) {
		// 获取当前root的子级
		String parentId = root.getParentCode();
		QueryWrapper<WorkProfession> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("profession_code", parentId);
		List<WorkProfession> nodeList = WorkProfessionMapper.selectList(queryWrapper);
		bomStructuresDTOList.add(root);
		for (WorkProfession node : nodeList) {
			List<WorkProfession> child = getPrioTree(node, bomStructuresDTOList);
		}

		// 递归退出条件没有下一层级
		if (nodeList == null || nodeList.size() == 0) {
			return Lists.newArrayList();
		}
		return nodeList;
	}
	
}
